package com.jsits.v2x.fs8.service.impl;

import com.jsits.v2x.common.dataClient.DataClient;
import com.jsits.v2x.common.dto.ControlDto;
import com.jsits.v2x.common.enums.NumberColor;
import com.jsits.v2x.common.enums.DevicePosition;
import com.jsits.v2x.fs8.config.Fs8Config;
import com.jsits.v2x.fs8.server.Fs8SocketServer;
import com.jsits.v2x.fs8.service.Fs8LedDisplayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class Fs8LedDisplayServiceImpl extends DataClient implements Fs8LedDisplayService, ApplicationListener<ApplicationReadyEvent> {
    @Autowired
    private Fs8Config fs8Config;

    private Integer testSpeed = 0;


    public List<ControlDto> testControls() {
        NumberColor c = testSpeed%2==0 ? NumberColor.GREEN : NumberColor.RED;
        List<ControlDto> controlDtos = new ArrayList<>();
        ControlDto controlDto1 = new ControlDto();
        controlDto1.setDevicePosition(DevicePosition.DEVICE_POSITION_MAIN_1);
        controlDto1.setNumberColor(c);
        controlDto1.setNumberContent(testSpeed);
        controlDtos.add(controlDto1);
        ControlDto controlDto2 = new ControlDto();
        controlDto2.setDevicePosition(DevicePosition.DEVICE_POSITION_MAIN_2);
        controlDto2.setNumberColor(c);
        controlDto2.setNumberContent(testSpeed);
        controlDtos.add(controlDto2);
        testSpeed++;
        return controlDtos;
    }

    /**
     * 获取各个显示屏需要显示的内容，并控制需要的屏幕显示
     */
    @Override
    public void updateLedScreen() {
        if(Fs8SocketServer.fs8Config == null) {
            return;
        }
        List<ControlDto> controlDtos = GetControlStatus(fs8Config.getDataUrl());
        //List<ControlDto> controlDtos = testControls();
        if(controlDtos != null) {
            for (ControlDto controlDto : controlDtos) {
                Fs8SocketServer.sendCtrontrolData(controlDto);
            }
        }
    }

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        Fs8SocketServer sc = new Fs8SocketServer();
        Fs8SocketServer.fs8Config = fs8Config;
        Thread thread = new Thread(sc);
        thread.start();
    }
}
